﻿/*
   Copyright 2013 Dmitry Bratus

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sample.DataContracts;
using System.Collections.Concurrent;
using Flower.Services.Data;
using Flower.Directory.Util;
using System.Xml;
using System.Threading.Tasks;

namespace Sample.Runner
{
    public partial class Program
    {
        private static void Callback()
        {
            const string serviceContainer =
                @"
<objects xmlns=""http://www.springframework.net"">
  <object id=""Queue"" type=""Flower.Client.QueueServiceClient, Flower.Client"">
    <constructor-arg name=""binding"">
      <object type=""System.ServiceModel.NetTcpBinding"">
        <!-- Binding properties. -->
      </object>
    </constructor-arg>
    <constructor-arg name=""remoteAddress"">
      <object type=""System.ServiceModel.EndpointAddress"">
        <constructor-arg value = ""net.tcp://localhost:11000/Flower/CallbackQueue"" />
      </object>
    </constructor-arg>
  </object>
</objects>";

            ServiceContainer sc = (ServiceContainer)_flowerClient.Directory.Get("/Services/Tests/Callback");

            if (sc == null)
            {
                var xDoc = new XmlDocument();
                xDoc.LoadXml(serviceContainer);

                sc = new ServiceContainer
                {
                    Name = "Callback",
                    Configuration = BlobUtil.WriteBlob(BlobFormat.TextXml, xDoc.WriteTo)
                };

                _flowerClient.Directory.Save("/Services/Tests", sc);
            }

            const int count = 1000;
            var request = new CallbackRequest
            {
                Recipient = Guid.NewGuid().ToString()
            };

            var results = new BlockingCollection<int>();
            _flowerClient.AddRecipient<int>(request.Recipient, i => results.Add(i));

            DateTime startTotal = DateTime.Now;

            try
            {
                DateTime start = DateTime.Now;

                for (int i = 0; i < count; i++)
                {
                    _flowerClient.StartProcess<CallbackRequest, object>("Tests/Callback", request, Log);
                }

                TimeSpan startProcessTotal = DateTime.Now - start;

                Console.WriteLine("Start: {0}.", startProcessTotal);
                Console.WriteLine("Start avg.: {0}.", TimeSpan.FromMilliseconds(startProcessTotal.TotalMilliseconds / count));

                start = DateTime.Now;
                results.Take();
                Console.WriteLine("First: {0}.", DateTime.Now - start);

                start = DateTime.Now;
                for (int i = 1; i < count; i++)
                {
                    results.Take();
                }

                TimeSpan responseTotal = DateTime.Now - start;
                Console.WriteLine("Response total: {0}.", responseTotal);
                Console.WriteLine("Avg.: {0}.", TimeSpan.FromMilliseconds(responseTotal.TotalMilliseconds / (count - 1)));

                Console.WriteLine("Total: {0}.", DateTime.Now - startTotal);
            }
            finally
            {
                _flowerClient.RemoveRecipient(request.Recipient);
            }
        }
    }
}
